﻿@{
    Layout = "~/Views/Shared/_Main.cshtml";
}
@using SRF.UI.Html
@section header{
    <link href="@Url.Content("~/Scripts/zTree/css/zTreeStyle/zTreeStyle.css")" rel="stylesheet" type="text/css"/>
    <script src="@Url.Content("~/Scripts/jquery-ui/js/jquery.ui.core.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-ui/js/jquery.ui.widget.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-ui/js/jquery.ui.mouse.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-ui/js/jquery.ui.draggable.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/bootstrap/js/bootstrap-modal.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/dialog.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/zTree/js/jquery.ztree.all-3.5.min.js")" type="text/javascript"></script>
    <style type="text/css">
        .leftpanel
        {
            float: left;
            width: 320px;
            padding-top:8px;
            border-top: 1px solid #eee;
            border-right: 1px solid #eee;
        } 
        .leftpanel select
        {
            width: 100%;
            overflow: hidden;
        }
        .rightpanel
        { 
            border-top: 1px solid #eee;
            padding-top:8px;
            padding-left:20px;
            float: left;
            width: 320px;
        }
        .ztree
        {
            min-height: 300px;
        }
    </style>
    <script type="text/javascript" language="javascript"> 
    @{ 
        var html = Html.TreeJson<SRF.Admin.Models.Permission>("permissionData", ViewBag.PermissionList as IList<SRF.Admin.Models.Permission>, m => m.Id, m => m.ParentId, m => m.Name);
        html += ";" + Html.TreeJson<SRF.Web.Areas.Admin.Models.TreeNode>("dataRuleData", ViewBag.DataRuleList as IList<SRF.Web.Areas.Admin.Models.TreeNode>, m => m.Id, m => m.ParentId, m => m.Name);
 
        @Html.Raw(html)
    } 
    </script>
    <script type="text/javascript">
        $(function () {
            var setting = {
                data: {
                    simpleData: {
                        enable: true,
                        rootPId: null
                    }
                }
            };
            var permissionTreeSetting = $.extend(true, {
                check: {
                    enable: true,
                    chkStyle: "checkbox",
                    nocheckInherit: true
                }
            }, setting);
            var dataRuleTreeSetting = $.extend(true, {
                check: {
                    enable: true,
                    chkStyle: "radio"
                }
            }, setting);

            $(dataRuleData).each(function () {
                this["nocheck"] = !this.pId;
            });
            $.fn.zTree.init($("#permissionlist"), permissionTreeSetting, permissionData);
            $.fn.zTree.init($("#datarulelist"), dataRuleTreeSetting, dataRuleData);
            //默认全部展开节点
            $.fn.zTree.getZTreeObj("permissionlist").expandAll(true);
            $.fn.zTree.getZTreeObj("datarulelist").expandAll(true);
            //
            $("#role").change(function () {
                onSelectRole($(this).val());
            })
            if ($("#role").val())
                onSelectRole($("#role").val());
            else
                $("#btnSave").attr("disabled", "disabled");
        })
        //选择角色
        function onSelectRole(roleId) {
            $("#tip").attr("class", "tip label-info").text("正在加载数据...").show();
            var url = "GetPermissions?random=" + Math.random();
            //加载功能项
            $.get(url, { roleId: roleId }, function (data) {
                var permissionList = $.fn.zTree.getZTreeObj("permissionlist");
                $.each(permissionList.getNodesByFilter(function () { return true; }), function (i, n) {
                    var checked = data && data.contains(this.id, function (a, b) {
                        return a.toLowerCase() == b.toLowerCase();
                    });
                    permissionList.checkNode(this, checked);
                })
            });
            //加载数据规则项
            url = "GetDataRuleItems?random=" + Math.random();
            $.get(url, { roleId: roleId }, function (data) {
                var dataRuleList = $.fn.zTree.getZTreeObj("datarulelist");
                $.each(dataRuleList.getNodesByFilter(function () { return true; }), function (i, n) {
                    var checked = data && data.contains(this.id, function (a, b) {
                        return a.toLowerCase() == b.toLowerCase();
                    });
                    dataRuleList.checkNode(this, checked);
                }) 
                $("#tip").hide();
            })
        }
        function expandTree(treeId, flag) {
            if (flag == undefined)
                flag = true;
            $.fn.zTree.getZTreeObj(treeId).expandAll(flag);
        }
        function save() {
            var roleId = $("#role").val();
            //获取功能授权项
            var permissionList = $.fn.zTree.getZTreeObj("permissionlist");
            var checkedNodes = permissionList.getCheckedNodes(true);
            var actions = [];
            for (var i = 0; i < checkedNodes.length; i++) {
                if (checkedNodes[i].check_Child_State == -1 && !checkedNodes[i].isParent)
                    actions.push(checkedNodes[i].id);
            } 
            //获取数据过滤项
            var dataRuleList = $.fn.zTree.getZTreeObj("datarulelist");
            checkedNodes = dataRuleList.getCheckedNodes(true);
            var dataRules = [];
            for (var i = 0; i < checkedNodes.length; i++) {
                if (checkedNodes[i].check_Child_State == -1 && !checkedNodes[i].isParent)
                    dataRules.push(checkedNodes[i].id);
            }

            $.ajax({
                url: "SaveAuth",
                data: { "roleId": roleId, "actions": actions, "dataRuleItems": dataRules },
                type: "POST",
                cache: false,
                success: function (result) {
                    $("#tip").attr("class","tip label-success").text("权限保存成功!").showTip(1200);
                },
                error: function () {
                    $("#tip").attr("class","tip label-error").text("权限保存失败!").showTip(3000);
                }
            });
        }
    </script>
}
<div style="margin-top: 0px; margin-bottom: 6px; border: 0px solid red">
    <div class="searchbar control-group" style="margin-bottom: 0px">
        <div class="controls" style="margin-left: 0px">
            <!--搜索条件-->
            <label class="" for="loginid" style="display: inline-block">
                选择角色:</label>
            @Html.DropDownList("role", ViewBag.RoleSelectList as IEnumerable<SelectListItem>)
            <div id="tip" class="tip" style="margin-left: 20px;">
                保存成功！</div>
            <input id="btnSave" type="button" class="btn btn-primary" style="float: right" value="保存" onclick="save();" /></div>
    </div>
</div>
<div>
    <div class="leftpanel"> 
        <span>功能列表</span><a href="javascript:expandTree('permissionlist');" style="display: inline-block;
            margin: 1px 3px; font-weight: bolder" title="全部展开">+</a><a href="javascript:expandTree('permissionlist',false);"
                style="display: inline-block; margin: 1px 3px; font-weight: bolder" title="全部折叠">-</a>
        <ul id="permissionlist" class="ztree">
        </ul>
    </div>
    <div class="rightpanel">
        <span>数据规则列表</span><a href="javascript:expandTree('datarulelist');" style="display: inline-block;
            margin: 1px 3px; font-weight: bolder" title="全部展开">+</a><a href="javascript:expandTree('datarulelist',false);"
                style="display: inline-block; margin: 1px 3px; font-weight: bolder" title="全部折叠">-</a>
        <ul id="datarulelist" class="ztree">
        </ul>
    </div>
</div>
